package com.example.demo.count;

import java.util.Arrays;

/**
 * 描述
 *
 * @author: yuy
 */
public class ArrayMoveRightTest {

    /**
     * 给定一个n位长的int数组，数组中奇数位不动，偶数位可以移动
     * 给定一个值t（t>0),请将偶数位的值向右移动t位。
     * 数据示例  [1,2,3,4,5,6,7,8]
     * t=1 [7,2,1,4,3,6,5,8]
     * t=3 [3,2,5,4,7,6,1,8]
     */

    public static void main(String[] args) {
        int[] data = {1, 2, 3, 4, 5, 6, 7, 8};
        System.out.println(Arrays.toString(data));
        swap(data, 1);
        System.out.println(Arrays.toString(data));

    }

    public static void swap(int[] arr, int k) {
        int nextValue[] = new int[arr.length];
        int sourceArr[] = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            sourceArr[i] = arr[i];
            if (i % 2 != 0) {
                nextValue[i] = i;
            } else {
                nextValue[i] = calcultateNextValue(arr, i, k % arr.length);
            }
        }
        for (int i = 0; i < arr.length; i++) {
            arr[nextValue[i]] = sourceArr[i];
        }
    }

    private static int calcultateNextValue(int[] arr, int i, int k) {
        int target = i;
        int count = 0;
        while (count < k) {
            target++;
            if (target == arr.length) {
                target = 0;
            }
            if (target % 2 == 0) {
                count++;
            }
        }
        return target;
    }
}
